//
//  ChongzhiViewController.m
//  HaoDeCaiFu
//
//  Created by 物联信息中心 on 2018/1/2.
//  Copyright © 2018年 郑开宏. All rights reserved.
//

#import "ChongzhiViewController.h"
#import "HDCFMyBankCardVc.h"
#import "GeneralMagager.h"
@interface ChongzhiViewController ()
@property (nonatomic, weak) UIView *hitView;
@property(nonatomic,strong)UIView *clearHudView;
@property(nonatomic,copy)NSString *tradePassword;
@property(nonatomic,copy)NSString *investorPayId;

@property (nonatomic, strong) NSDictionary *bankDetail;
@property (nonatomic, strong) NSArray *bankArray;

@end

@implementation ChongzhiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBarHidden=NO;
    self.edgesForExtendedLayout = UIRectEdgeNone; self.longLine.backgroundColor=ZKcolor(240, 240, 240);
    self.duanLine.backgroundColor=ZKcolor(240, 240, 240);
    self.chongzhiBtn.layer.cornerRadius=3;
    self.chongzhiBtn.backgroundColor=COLOR_NAV;
    self.title=@"充值";
    [self initData];
}


- (void)confirmClickWithPassWord:(NSString *)password{
    self.tradePassword = password;
    [self.clearHudView removeFromSuperview];
    [self chongzhi];
    
    
}
- (IBAction)chongzhiBtnClick:(id)sender {
    
    
    [self.view endEditing:YES];
    
    if (!self.jineTF.text||[self.jineTF.text isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"请输入充值金额"];
        return;
    }
    
    if( ![GeneralMagager isPureInt:self.jineTF.text] || ![GeneralMagager isPureFloat:self.jineTF.text])
    {
        [SVProgressHUD showInfoWithStatus: @"警告:含非法字符，请输入纯数字！"];
        return;
    }
    
    
    [self alertPasswordView];
}
- (IBAction)chooseBankBtnClick:(id)sender {
    
    HDCFMyBankCardVc*vc=[[HDCFMyBankCardVc alloc]init];
    vc.isNeedBlock=YES;
    vc.isXianshiWallet=NO;
    vc.isHiddenDeleteBtn = YES;
    vc.block = ^(NSString *name, NSString *bank, NSString *bankId,BOOL isBankCard) {
        self.investorPayId=bankId;
        self.bankLable.text = [NSString stringWithFormat:@"%@ *** %@", name, bank];
    };
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)alertPasswordView
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *view = [[UIView alloc] init];
    self.hitView = view;
    view.backgroundColor=[UIColor colorWithRed:30/250 green:30/250 blue:30/250 alpha:0.5];
    UITapGestureRecognizer *r5 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewHit)];
    r5.numberOfTapsRequired = 1;
    [view addGestureRecognizer:r5];
    
    view.frame = CGRectMake(0, 0, screenWidth, screenHeight);
    //    view.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1];
    InputPassWordView *inputView = [InputPassWordView InputPassWordView];
    inputView.frame = CGRectMake(30, (screenHeight - 220) / 2, screenWidth - 60, 182);
    inputView.layer.cornerRadius = 5.0;
    inputView.layer.shadowColor = ZKcolor(100, 100, 100).CGColor;//shadowColor阴影颜色
    
    inputView.layer.shadowOffset = CGSizeMake(0,0);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    inputView.layer.shadowOpacity = 0.7;//阴影透明度，默认0
    inputView.layer.shadowRadius = 3;//阴影半径，默认3
    inputView.clipsToBounds = YES;
    inputView.delegate = self;
    
    [view addSubview:inputView];
    [window addSubview:view];
}
- (void)cancelClick{
    [self.hitView removeFromSuperview];
}
- (void)viewHit{
    [self.hitView removeFromSuperview];
}

- (void)initData{
    
    NSDictionary *paramet = @{
                              KparametTwo:@"/public/investor/find-bank"
                              };
    [HttpRequest sendGetRequestWithURL:nil WithParamets:paramet success:^(id result) {
        
        NSDictionary *dict = result[@"data"];
        self.bankArray = [NSArray arrayWithArray:dict[@"data"]];
        LSWeakSelf;
        [weakSelf getBankCardInfo];
        
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

- (void)getBankCardInfo{
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
    paramets[@"accountNumber"] = kAcount;
    NSString *jsonStr = [self dictionaryToJson:paramets];
    NSDictionary *paramet = @{KparametOne:jsonStr,
                              KparametTwo:KBankCardInfo};
    [HttpRequest sendGetRequestWithURL:nil WithParamets:paramet success:^(id result) {
        NSString *code = result[@"code"];
        if ([code isEqualToString:@"0000"]) {
            NSDictionary *dataDict = result[@"data"];
            
            NSLog(@"%@",result);
            
            NSDictionary *investorBankDetails = dataDict[@"investorBankDetails"];
            NSArray *bankArrayt = investorBankDetails[@"data"];
            NSDictionary *bankDetail = bankArrayt.firstObject;
            self.bankDetail = bankDetail;
            //            NSString *imageName = [NSString stringWithFormat:@"icon_%@", bankDetail[@"bankCode"]];
            //            self.bankIcon.image = [UIImage imageNamed:imageName];
            for (int i = 0; i < self.bankArray.count; i++) {
                NSDictionary *dict = self.bankArray[i];
                if ([bankDetail[@"bankCode"] isEqualToString:dict[@"bankCode"]]) {
                    //                    NSString *banName = dict[@"bankName"];
                    NSString *cardNo = bankDetail[@"bankNumber"];
                    NSString *laterFour = [cardNo substringFromIndex:(cardNo.length - 4)];
                    self.investorPayId=[NSString stringWithFormat:@"%@",bankDetail[@"investorPayId"]];
                    self.bankLable.text = [NSString stringWithFormat:@"%@ *** %@", dict[@"bankName"], laterFour];
                    break;
                }
            }
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"message"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
        
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"获取银行卡信息失败"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}


-(void)chongzhi
{

    
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
    paramets[@"accountNumber"] = kAcount;
     paramets[@"investmentAmount"] = self.jineTF.text;
    paramets[@"investorPayId"] = self.investorPayId;
    paramets[@"password"] = self.tradePassword;
    paramets[@"payMethod"] = @"0";
    paramets[@"currency"] = @"156";
    paramets[@"merchantNumber"] = [GeneralMagager createLiuShuiHao];
    NSString *jsonStr = [self dictionaryToJson:paramets];
    NSDictionary *paramet = @{
                              KparametTwo:@"/public/wallet/recharge-cash-wallet"};
    [SVProgressHUD show];
    LSWeakSelf;
    [HttpRequest sendPostRequestWithURL:nil WithParamets:paramet WithJsonString:jsonStr success:^(id result) {

        NSString *code = result[@"code"];
        if ([code isEqualToString:@"0000"]) {
            [SVProgressHUD dismiss];
        
            NSMutableArray*dataArray=[result objectForKey:@"data"];
            if (dataArray.count!=0) {
                NSDictionary*dic=dataArray[0];
                NSString*transactionStatus=[dic objectForKey:@"transactionStatus"];
                if ([transactionStatus isEqualToString:@"received"]) {
                    [SVProgressHUD showSuccessWithStatus:@"充值成功"];
                    [SVProgressHUD dismissWithDelay:1];
                    [weakSelf viewHit];
                     [weakSelf.navigationController popViewControllerAnimated:YES];
                    
                }
                else
                {
                    NSString*reason=[dic objectForKey:@"reason"];
                    [weakSelf viewHit];
                    [SVProgressHUD showInfoWithStatus:reason];
                }
                
            }
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:result[@"message"]];
            [SVProgressHUD dismissWithDelay:2.0];
        }

    } failure:^(NSError *errer) {

        [SVProgressHUD showErrorWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}


- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
